/*
 *@本js功能：镜像管理
 *@本js作者：杨锡君
 *@编写时间：2018年06月21日
 */
$(function() {
    var Alert1 = new Alert();
    var confirm1 = new Confirm();
    var loading1 = new Loading();
    loading1.init();
    var $tableBox = $(".tableBox");
    var th = "<tr>" +
        "<th class=\"col-lg-1\">序号</th>" +
        "<th class=\"col-lg-4\">镜像</th>" +
        "<th class=\"col-lg-4\">来源</th>" +
        "<th class=\"col-lg-2\">状态</th>" +
        "<th class=\"col-lg-1\">操作</th>" +
        "</tr>";
    function tabelModel(opt,startRow) {
        var statusCode = opt.statusCode;
        if(statusCode == 1){
            statusCode = "使用";
        }else{
            statusCode = "未使用";
        }
        var str = "<tr data-num=\"0\">" +
            "<td class=\"col-lg-1\">"+ startRow +"</td>" +
            "<td class=\"col-lg-4\" title="+ opt.name +">"+ opt.name +"</td>" +
            "<td class=\"col-lg-4\" title="+ opt.imageSource +">"+ opt.imageSource +"</td>" +
            "<td class=\"col-lg-2\" title="+ statusCode +">"+ statusCode +"</td>" +
            "<td class=\"col-lg-1\">";
        if(opt.imageSource != "预置"){
            str+="<a href=\"javascript:;\" data-status='"+ opt.statusCode +"' data-id='"+ opt.id +"'   class=\"detailBtn\">删除</a>";
        }
        str+="</td>"+
            "</tr>";
        return str;
    }
    var virMachineSch = {
        status:false,
        init: function() {
            this.bindEvent();
        },
        bindEvent: function() {
            var self = this;
            var json = {
                "pageNum": 1,
                "pageSize": 10
            };
            self.tabelList(json);
        },
        exchange:function(json){
            var self = this;
            var jsonS = json;
            $(".detailBtn").off("click").click(function () {
                var id = Number($(this).attr("data-id"));
                var json = {"id":id};
                confirm1.content = "确定删除当前镜像吗？";
                json.isForced = false;
                confirm1.success = function () {
                    loading1.show();
                    $.ajax({
                        type: "get",
                        url: "manage/image/delete",
                        data:json,
                        async: true,
                        success: function (msg) {
                            loading1.hide();
                            if(msg.code == 200) {
                                Alert1.content = "删除镜像成功";
                                Alert1.success = function () {
                                    self.tabelList(jsonS);
                                };
                                self.status = false;
                                Alert1.init();
                                Alert1.show();
                            }else if(msg.code == 500401){
                                confirm1.content = msg.message;
                                confirm1.success = function () {
                                    json.isForced = true;
                                    loading1.show();
                                    $.ajax({
                                        type: 'get',
                                        url: "manage/image/delete",
                                        async: true,
                                        data: json,
                                        success: function (msg) {
                                            loading1.hide();
                                            if (msg.code == 200) {
                                                Alert1.content = "删除成功";
                                                Alert1.success = function () {
                                                    window.location.reload(true);
                                                };
                                                Alert1.init();
                                                Alert1.show();
                                            }else{
                                                Alert1.content = msg.message;
                                                Alert1.init();
                                                Alert1.show();
                                            }
                                        }
                                    });
                                };
                                confirm1.init();
                                confirm1.show();
                            } else {
                                Alert1.content = msg.message;
                                self.status = true;
                                Alert1.init();
                                Alert1.show();
                                return false;
                            }
                        }
                    });
                };
                confirm1.init();
                confirm1.show();
            });
        },
        tabelList:function(jsons) {
            var self = this;
            loading1.show();
            // json = JSON.stringify(jsons);
            $.ajax({
                type: "get",
                url: "manage/image/page",
                data: jsons,
                async: true,
                success: function(msg) {
                    loading1.hide();
                    // msg = JSON.parse(msg);
                    if(msg.code == 200) {
                        $tableBox.empty();
                        if(msg.data.list.length != 0) {
                            //列表
                            var tabel = "";
                            var startRow = msg.data.startRow;
                            $.map(msg.data.list, function(opt) {
                                tabel += tabelModel(opt,startRow);
                                startRow++;
                            });
                            $tableBox.append(th + tabel);
                            self.exchange(jsons);
                        } else {
                            var trImg = "<tr><td colspan='5'><img style='margin: 80px auto 20px;' src='pc/images/ico_noneData.png'><p style='font-size:26px;color:#8a8a8a;width:100%;text-align:center;'>暂无数据</p></td></tr>";
                            $tableBox.append(th + trImg)
                        }
                        var sumpage = msg.data.pages;
                        if(sumpage == 0){
                            sumpage = 1;
                        }
                        var nowpage = 1;
                        var pagenumber = 10;
                        $(".tcdPageCode").createPage({
                            pageCount: sumpage, //总页数
                            current: nowpage, //当前页数
                            pageNumber: pagenumber, //每页显示数量
                            backFn: function(p) {
                                nowpage = p;
                                if(nowpage >= sumpage) {
                                    nowpage = sumpage;
                                }
                                var jsonStr = jsons;
                                jsonStr.pageNum = nowpage;
                                loading1.show();
                                $.ajax({
                                    type: "get",
                                    url: "manage/image/page",
                                    data: jsonStr,
                                    async: true,
                                    success: function(msg) {
                                        //列表
                                        loading1.hide();
                                        // msg = JSON.parse(msg);
                                        $tableBox.empty();
                                        var tabel = "";
                                        var startRow = msg.data.startRow;
                                        $.map(msg.data.list, function(opt) {
                                            tabel += tabelModel(opt,startRow);
                                            startRow++;
                                        });
                                        $tableBox.append(th + tabel);
                                        self.exchange(jsonStr);
                                    }
                                });
                            }
                        });
                    } else {
                        Alert1.content = msg.message;
                        Alert1.init();
                        Alert1.show();
                        return false;
                    }

                }
            });

        }
    };
    virMachineSch.init();
});